#include <QApplication>

#include "LVGLSimulator.h"
#include "MainWindow.h"

#include <erpc_client_setup.h>
#include <erpc_port.h>
#include <erpc_version.h>
#if 11200 <= ERPC_VERSION_NUMBER
#include "c_lvgl_is_designer_client.h"
#else
#include "lvgl_is_designer.h"
#endif
#include "lvgldesigner.h"

#define USE_TCP_TRANSPORT

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication a(argc, argv);
    int ret;

#ifdef USE_TCP_TRANSPORT

    LVGLDesigner::transport = erpc_transport_tcp_init(LVGL_HOST, 5555, false);
#else
    LVGLDesigner::transport = erpc_transport_serial_init("COM6", 115200);
#endif
    if (LVGLDesigner::transport == NULL) {
        printf("erpc transport init error\n");
        return 0;
    }
    erpc_mbf_t message_buffer_factory = erpc_mbf_dynamic_init();
    erpc_client_t client = erpc_client_init(LVGLDesigner::transport, message_buffer_factory);
    if (client == NULL) {
        printf("erpc client init error\n");
        return 0;
    }
    initISDesigner_client(client);

    MainWindow w;
    LVGLKeyPressEventFilter filter(w.simulator(), &a);
    a.installEventFilter(&filter);

    w.show();

    ret = a.exec();

    w.clearLvgl();
    deinitISDesigner_client();
#ifdef USE_TCP_TRANSPORT
    if (LVGLDesigner::transport != NULL) {
#if 11200 <= ERPC_VERSION_NUMBER
        erpc_transport_tcp_close(LVGLDesigner::transport);
#else
        erpc_transport_tcp_close();
#endif
    }
#else
#endif
  return ret;
}
